package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * Created by Czl on 2015/7/25.
 */
@SuppressWarnings("serial")
public class CourseStudent extends Model<CourseStudent> {

    public static final CourseStudent dao = new CourseStudent();

    private final String target_type = "studyplan"; 
    
    /**
           *   保存学生选课信息
     * @param courseStudent
     * @param courseId
     * @param studentId
     * @param courseType
     */
    public void saveCourseStudent(CourseStudent courseStudent,
    		Long courseId, Long studentId, Long targetId,
    		String targetType, String courseType,
    		Long courseTopicId, String studyGroup) {
        courseStudent.set("course_id",courseId);
        courseStudent.set("student_id",studentId);
        courseStudent.set("course_type",courseType);
        courseStudent.set("create_time",new Date());
        courseStudent.set("study_group", studyGroup);
        courseStudent.set("target_id", targetId);
    	courseStudent.set("target_type", targetType);
    	courseStudent.set("course_topic_id", courseTopicId);
        courseStudent.set("learned_hour", 0);
        courseStudent.set("learned_status", 0);
        courseStudent.set("learned_times", 0);
        courseStudent.remove("id").save();
    }

    /**
     * 查询是否选课
     * @param courseId
     * @param studentId
     * @return
     */
    public CourseStudent findByCourseIdAndStudentId(Long courseId,Long studentId){
        return dao.findFirst("select * from t_course_student where course_id = ? and student_id = ?",courseId,studentId);
    }

    /**
     * 分页查询学员选课信息
     * @param pageNumber
     * @param pageSize
     * @param sql
     * @param paras
     * @return
     */
    public Page<CourseStudent> findCourseStudentPage(int pageNumber,int pageSize,String sql,Object[] paras){
        return dao.paginate(pageNumber, pageSize, "select t.* ", sql, paras);
    }
    
    /**
     * @param studyPlanId
     * @param courseTopicId
     * @param courseId
     * @param studentId
     * @return
     */
	public CourseStudent getCourseTopicCourseStudent(Long studyPlanId, 
			Long courseTopicId, Long courseId, Long studentId, String targetType) {
		String sql = "select * from t_course_student where student_id = " + studentId;
    	if (studyPlanId>0) {
    		sql += " and target_id = " + studyPlanId + " and target_type = '" + targetType + "'";
    	}
    	if (courseId>0) {
    		sql += " and course_id = " + courseId;
    	}
    	if (courseTopicId>0) {
    		sql += " and course_topic_id = " + courseTopicId;
    	}
    	
    	List<CourseStudent> list = dao.find(sql);
    	if (list!=null && list.size()>0) {
    		return list.get(0);
    	}
    	
    	return null;
	}
    
    public List<CourseStudent> findCourseStudentList(Long studentId,
    		Long studyplanId, String targetType, int status, int courseType) {
    	String sql = "select * from t_course_student where student_id = " + studentId;
    	if (studyplanId>0) {
    		sql += " and target_id = " + studyplanId + " and target_type = '" + targetType + "'";
    	}
    	// '0 未完成  1已完成'
    	if (status!=-1) {
    		sql += " and learned_status = " + status;
    	}
    	// '0选修课  1必修课 '
    	if (courseType!=-1) {
    		sql += " and course_type = " + courseType;
    	}
    	
        return dao.find(sql);
    }
    
    /**
     * 查询选修课和必修课门数
     * @param studentId
     * @param courseType
     * @param isPassed
     * @return
     */
    public Long findCountMyCourse(Long studentId,String courseType,int isPassed){
        return Db.queryLong("select count(id) from t_course_student " +
                " where student_id = ? and course_type = ? and is_passed = ?",studentId,courseType,isPassed);

    }


    public List<CourseStudent> findByStudentId(Long studentId) {
        return dao.find("select * from t_course_student where student_id = ?",studentId);
    }
    
    /**
     * 学员学习总时长
     * @param studentId
     * @return 单位秒数
     */
	public long getTotalCourseLearnedTime(long studentId) {
		String sql = "select sum(learned_hour) from t_course_student where student_id = ?";
		BigDecimal result = Db.queryBigDecimal(sql, studentId);
		return result.longValue();
	}
	
}
